//index.js
//获取应用实例
const app = getApp()
const{
  taskList,getWorkTask
} =require('../../api/task')

Page({
  data: {
    is_work:false,
    go_work_text:'上班考勤',
    down_work_text:'下班考勤',
    is_go_work:false,
    is_down_work:false,
    show_task_release: false,
    motto: '欢迎使用京绿通',
    minDate: new Date(2020, 10, 1).getTime(),
    userInfo: {},
    category:'0',
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    page:1,
    pageSize:10,
    finished: false,
    task_list:[],
    active: '2',
    task_date_list: ['2020-11-29','2020-12-04'],
    formatter(day) {
      let year = day.date.getFullYear();
      let month = day.date.getMonth() + 1;
      let date = day.date.getDate();
      let str_date = ''+year+'-'+month+'-'+date

      if (month === 5) {
        if (date === 1) {
          day.topInfo = '劳动节';
        } else if (date === 4) {
          day.bottomInfo = '五四青年节';
        } else if (date === 11) {
          day.text = '今天';
        }
      }

      if (day.type === 'start') {
        day.bottomInfo = '入住';
      } else if (day.type === 'end') {
        day.bottomInfo = '离店';
      }

      return day;
    },
  },
  formatDate(date) {
    let new_date = new Date(date);
    let year = new_date.getFullYear();
    let month = new_date.getMonth() + 1;
    let s_date = new_date.getDate();
    if (month < 10) { month = '0'+month}
    if (s_date < 10) { s_date = '0'+s_date}
    let str_date = ''+year+'-'+month+'-'+s_date
    let task_cout = 0
    let tmp_project_name = ''
    let tmp_is_desc = 0
    let tmp_task_info_id = 0
    let tmp_id = 0
    let time_str_date = new Date(str_date).getTime() + 24*60*60*1000 + 1;
    
    let time_now_value = new Date().getTime();
    let is_nine_task = false
    this.data.task_list.forEach(function(item, index) {
      if (str_date == item.task_info.finish_date) {
        if ((item.task_info.maintain_id == '90011' || item.task_info.maintain_id == '90012') && time_str_date < time_now_value) { //过期
          is_nine_task = true
        } else {
          task_cout++
          tmp_project_name = item.projectName
          tmp_task_info_id = item.task_info.id
          tmp_is_desc = item.task_info.is_desc
          tmp_id = item.id
        }
      }
      console.log(item)
    });
    console.log(task_cout)
    if (task_cout == 1) {
      wx.navigateTo({
        url: `/pages/canvas/index?pn=${tmp_project_name}&id=${tmp_id}&task_id=${tmp_task_info_id}&is_desc=${tmp_is_desc}`,
      })
    } else if (task_cout > 1) {
      this.setData({
        active: "2"
      })
    } else {
      if (is_nine_task) {
        wx.showModal({
          title: "温馨提示", // 提示的标题
          content: "抱歉，考勤已经截止了!", // 提示的内容
          showCancel: false, // 是否显示取消按钮，默认true
          confirmText: "确定", // 确认按钮的文字，最多4个字符
          confirmColor: "#576B95", // 确认按钮的文字颜色，必须是 16 进制格式的颜色字符串
          success: function (res) {
            return
          }
        })
      } 
    }
    

    //return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  doGoWorkTask(event) {
    if(this.data.is_work && !this.data.is_go_work) {
      wx.navigateTo({
        url: `/pages/canvas/index?pn=&id=0&task_id=0&is_desc=false&do_type=4`,
      })
    }
    
  },
  doDownWorkTask(event) {
    if(this.data.is_work && !this.data.is_down_work) {
      wx.navigateTo({
        url: `/pages/canvas/index?pn=&id=0&task_id=0&is_desc=false&do_type=5`,
      })
    }
    
  },

  onClickToPhotoTask() {
    wx.navigateTo({
      url: `/pages/canvas/index?pn=&id=0&task_id=0&is_desc=false&do_type=3`,
    })
  },
  onConfirm(event) {
    this.formatDate(event.detail)
  },
  tabclick(e){
    this.data.category = e.detail.name
    if (e.detail.name == 2) {
      this.data.category = 0
    }
    this.data.page=1
    this.data.task_list=[]
    this.init()
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onClickToReleaseTask(){
    wx.navigateTo({
      url: '/pages/releaseTask/index',
    })
  },
  onShow: function () {
    this.setData({
      active: app.showTask
    })
  },
  onShow: function(){
    this.onLoad();
  },
  onLoad: function () {
    console.log("任务页面加载。。。");
    wx.showLoading({
      title: '数据加载中',
    })
    
    if (app.userInfo) {
      this.setData({
        userInfo: app.userInfo,
        hasUserInfo: true
      })
      this.init()
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.init()
        app.userInfo = res.data
        this.setData({
          userInfo: res.data,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.userInfo = res.data
          this.setData({
            userInfo: res.data,
            hasUserInfo: true
          })
          if (this.userInfoReadyCallback) {
            this.userInfoReadyCallback(res)
          }
        }
      })
    }
    
  },
  init() {
    let obj = {
      pageNo:this.data.page,
      isFinish:this.data.category,
      pageSize:this.data.pageSize
    }
    if (this.data.category == 0) {
      getWorkTask().then(res=>{
        if(res.data.result){
          this.setData({
            down_work_text:'下班考勤',
            go_work_text:'上班考勤',
            is_work:true,
            is_go_work:res.data.is_go_work,
            is_down_work:res.data.is_down_work,
          })
        }else{
          this.setData({
            down_work_text:'',
            go_work_text:'',
            is_work:'',
            is_go_work:'',
            is_down_work:'',
          })
        }
        
      })
    }   
    taskList(obj).then(res=>{
      let templateList=res.data.list
      if(!templateList.length>0){
        this.data.finished=true
      }


      if(templateList && templateList.length){
        this.data.task_list=[...this.data.task_list,...templateList]

      }
      this.setData({
        task_list:this.data.task_list ||[]
      })
      if (this.data.category == 1) {
        let task_list_day = []
        this.data.task_list.forEach(function(item, index) {
          task_list_day.push(item.task_info.finish_date)
        });
        this.setData({
          task_date_list:task_list_day
        })
        const _this = this
        _this.setData({
          signInList:res,
          // 签到状态格式化
          formatter: function(day){
            let year = day.date.getFullYear();
            let month = day.date.getMonth() + 1;
            let date = day.date.getDate();
            if (month < 10) { month = '0'+month}
            if (date < 10) { date = '0'+date}
            let str_date = ''+year+'-'+month+'-'+date

            _this.data.task_list.forEach(function(item, index) {
              if (item.task_info.finish_date == str_date) {
                day.topInfo = item.task_info.maintain_sub_name
                if (item.task_info.is_finish) {
                  day.bottomInfo = '已完成';
                } else {
                  day.type = 'selected'
                  day.bottomInfo = '截止';
                }
              }
            });
           return day
         }
        })
      }
      wx.hideLoading({
        complete: (res) => {},
      })
      wx.hideNavigationBarLoading(); //完成停止加载图标
      wx.stopPullDownRefresh();
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  searchPage(){
    wx.navigateTo({
      url: '/pages/search/index',
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.data.page=1
      this.data.task_list=[]
      this.data.finished=false
      this.init()

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.finished){
      this.data.page=parseInt(this.data.page+1)
      this.init()
    }
  },

})
